package bold.module.rabbitmq;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * @date 2019-05-28
 * @auther likailun
 **/
public class Producer {

    /**
     * Exchange 类型
     * direct(直连交换机) routing key 路由键 如果和 Binding 中的 binding key 绑定键 必须完全相同
     * fanout 传播
     * topic  路由键 用. 分隔开；绑定键 支持 # * 匹配
     */
    public static void main(String[] args) throws Exception {
        Connection connection = getConnection();
        Channel channel = connection.createChannel();
        channel.exchangeDeclare(Constant.exchange_name, BuiltinExchangeType.DIRECT);

//        channel.confirmSelect();
//        channel.addConfirmListener();
//        channel.addReturnListener();
        for (int i = 1; i <= 5; i++) {
            System.out.println("发送消息：" + i);
            channel.basicPublish(Constant.exchange_name, Constant.routing_key, null, String.valueOf(i).getBytes());
        }
        channel.close();
        connection.close();


    }

    public static Connection getConnection() throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("127.0.0.1");
        factory.setUsername("admin");
        factory.setPassword("111111");
//        factory.setVirtualHost();
        Connection connection = factory.newConnection();
        return connection;
    }

}
